#include "networkcontrol.h"

NetworkControl::NetworkControl(Coordinator* c,QObject *parent) :
    QObject(parent)
{
  coord=c;
  clientCtrl=NULL;
  doc=NULL;
  server.listen(QHostAddress::Any, 2201);
  if(!server.isListening()){
    qDebug()<<"Could not bind";
    return;
  }
  connect(&server, SIGNAL(newConnection()), this, SLOT(newConnection()));
  netTimer.setInterval(10000);
  connect(&netTimer, SIGNAL(timeout()), this, SLOT(trigger()));


}

void NetworkControl::newConnection(){
    if(clientCtrl==NULL || !clientCtrl->isOpen()){
        clientCtrl=server.nextPendingConnection();
        connect(clientCtrl,SIGNAL(disconnected()),this, SLOT(disconnected()));
        fullDump();
        QString data(QChar(2));
        data.append (doc->toString());
        data.append (QChar(3));
        clientCtrl->write(data.toAscii());
        netTimer.start();
    }else{
        //handle only one controll connection;
        qDebug()<<"handle only one controll connection";
        server.nextPendingConnection()->close();
    }
}

void NetworkControl::disconnected(){
    //delete clientCtrl;
    clientCtrl=NULL;
    netTimer.stop();
}

QDomDocument* NetworkControl::fullDump(){
    if(doc!=NULL){
        delete doc;
    }
    doc=new QDomDocument("shareDownload");
    QDomElement root=doc->createElement("root");
    doc->appendChild(root);
    root.appendChild(dumpAllLinks());
    root.appendChild(dumpAllProxy());

    return doc;
}

QDomDocument* NetworkControl::updateDump(){
    if(doc!=NULL){
        delete doc;
    }
    doc=new QDomDocument("shareDownload");
    QDomElement root=doc->createElement("root");
    doc->appendChild(root);
    root.appendChild(dumpUpdatedLinks());
    root.appendChild(dumpUpdatedProxy());

    return doc;
}


QDomElement NetworkControl::dumpAllLinks(){
    QVector<Link*> files=coord->getLinkFactory()->getAllLinks();
    QDomElement links=doc->createElement("links");
    foreach(Link* l, files){
        QDomElement el=l->xmlDesc(doc);
        links.appendChild(el);
    }
    return links;
};

QDomElement NetworkControl::dumpAllProxy(){
    QVector<ProxyDesc*> prxs=coord->getProxyFactory()->getAllProxies();
    QDomElement proxies=doc->createElement("proxies");
    foreach(ProxyDesc* l, prxs){
        QDomElement el=l->xmlDesc(doc);
        proxies.appendChild(el);
    }
    return proxies;
};

QDomElement NetworkControl::dumpUpdatedLinks(){
    QVector<Link*> files=coord->getLinkFactory()->getAllLinks();
    QDomElement links=doc->createElement("links");
    foreach(Link* l, files){
        if(l->changed){
            QDomElement el=l->xmlDesc(doc);
            links.appendChild(el);
        }
    }
    return links;
};

QDomElement NetworkControl::dumpUpdatedProxy(){
    QVector<ProxyDesc*> prxs=coord->getProxyFactory()->getAllProxies();
    QDomElement proxies=doc->createElement("proxies");
    foreach(ProxyDesc* l, prxs){
        if(l->changed){
            QDomElement el=l->xmlDesc(doc);
            proxies.appendChild(el);
        }
    }
    return proxies;
};

void NetworkControl::trigger(){
    if(clientCtrl==NULL)return;
    updateDump();
    QString data(QChar(2));
    data.append (doc->toString());
    data.append (QChar(3));
    clientCtrl->write(data.toAscii());
}
